<?php
namespace Admin\Controller;
use Common\Controller\AdminCommonController;
use Common\Org\Util\InputData;

class Managecontroller extends AdminCommonController {
    private $_model;
    function _initialize(){
        parent::_initialize();
        $this->_model = D('Manage');
    }

    /**
     * 管理员列表
     */
    public function index(){
        $list = $this->_model->lists(10);
        $this->assign($list);
        $this->display();
    }

    /*
     * 删除用户信息，同时删除组信息
     * */
    public function del(){
        if(IS_GET){
            $id = I("get.id",0,'trim');
            empty($id) && $this->returnData('请选择操作项！');

            $type = M('manage')->where('id='.$id)->getField('type');
            M()->startTrans();
            $result = $this->_model->del($id);
            $result_two = $this->_model->delGroupByUid(array('uid'=>$id));
            if($result && $result_two){
                M()->commit();
                $this->clearCache($type);
                $this->addHistoryLog('删除系统账号成功！');
                $this->returnData('删除成功！',1);
            }else{
                M()->rollback();
                $this->returnData('删除失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /*
     * 启用或者禁用用户信息
     * */
    public function setStatus(){
        if(IS_GET){
            $id = I('get.id');
            empty($id) && $this->returnData('请选择操作项！');

            $status = M('manage')->where('id='.$id)->getField('status');
            $status = $status == 1 ? 0 : 1;

            if($this->_model->setStatus($id,$status,'status')){
                $this->addHistoryLog('更改系统账号状态成功！');
                $this->returnData('操作成功！',1);
            }else{
                $this->returnData('操作失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /*
     * 有用户id弹出带信息页面，没有则空页面
     * */
    public function edit(){
        $id = I("get.id",0,'intval');
        if($id){
            $info = $this->_model->detail($id);
            $this->assign('info',$info);
        }

        $group_list = $this->_model->group_list();
        $this->assign('group_list',$group_list);
        $this->display();
    }

    /*
     * 更新或者保存用户信息
     * */
    public function update(){
        if(IS_POST){
            $data = $this->returnPostData();

            $where = array();
            $where['account'] = $data['account'];
            if($data['id']){
                $where['id'] = array('neq',$data['id']);

                if($this->_model->where($where)->getField('id')){
                    $this->returnData('该账号已存在！');
                }

                $data['edit_time'] = NOW_TIME;

                M()->startTrans();
                $result = $this->_model->updateGroupAccess(array('uid'=>$data['id'],'group_id'=>$data['role_id']),$data['id']);

                $data['type'] = $data['role_id'];
                unset($data['role_id']);
                $result_two = $this->_model->updateManage($data);

                if($result >= 0 && $result_two){
                    M()->commit();
                    $this->clearCache($data['type']);
                    unset($where);
                    unset($data);
                    $this->returnData('编辑成功！',1,U('Manage/index'));
                }else{
                    M()->rollback();
                    $this->returnData('编辑失败！');
                }
            }else{

                if($this->_model->where($where)->getField('id')){
                    $this->returnData('该账号已存在！');
                }

                $data['add_time'] = NOW_TIME;
                $data['edit_time'] = NOW_TIME;
                $role_id = $data['role_id'];
                $data['type'] = $data['role_id'];
                unset($data['role_id']);
                M()->startTrans();
                $result = $this->_model->updateManage($data);

                $data = array();
                $data["uid"]=$result;
                $data["group_id"]=$role_id;
                $result_two = $this->_model->updateGroupAccess($data);

                if($result && $result_two){
                    M()->commit();
                    $this->clearCache($data['type']);
                    unset($where);
                    unset($data);
                    $this->returnData('添加成功！',1,U('Manage/index'));
                }else{
                    M()->rollback();
                    $this->returnData('添加失败！');
                }
            }
        }else{
            $this->returnData('请求方式错误');
        }
    }

    /**
     * 返回数据
     * @return array
     */
    private function returnPostData(){
        $id = I('post.id');
        $account = I('post.account','','trim');
        $que_password = I('post.que_password','','trim');
        $new_password = I('post.new_password','','trim');
        $status = I('post.status');
        $role_id = I('post.role_id');
        $nickname = I('post.nickname','','trim');
        //$email = I('post.email','','trim');

        empty($account) && $this->returnData('账号不为空！');
        if(empty($id)){
            empty($new_password) && $this->returnData('新密码不为空！');
            if((strlen($new_password)<5 ||strlen($new_password)>15)){
                $this->returnData('密码长度为5~15位!');
            }
            if(($new_password != $que_password)){
                $this->returnData('密码不一致！');
            }
        }

        $where = array();
        !empty($id) && $where['id'] = array('neq',$id);

        empty($nickname) && $this->returnData('请输入昵称！');
        if(abslength($nickname) > 30){
            $this->returnData('昵称长度不超过30个字符！');
        }

        /*if(empty($email)){
            $this->returnData('邮箱不能为空');
        }
        if(!preg_match(InputData::$email_reg,$email)){
            $this->returnData('邮箱格式错误');
        }
        if(strlen($email)>40){
            $this->returnData('邮箱不能超过40个字符');
        }*/

        empty($role_id) && $this->returnData('请选择角色！');

        $data = array(
            'account'=>$account,
            //'email'=>$email,
            'add_mid'=>$_SESSION[C('USER_AUTH_KEY')],
            'nickname'=>$nickname,
            'status'=>$status,
            'role_id'=>$role_id,
        );

        if(empty($id)){
            $data['password'] = md5($que_password);
        }else{
            $data['id'] = $id;
        }

        return $data;

    }

    /**
     * 重置密码
     */
    public function resetPassword(){
        if(IS_GET){
            $id = I('get.id');
            empty($id) && $this->returnData('请选择操作项！');

            $model = M('manage');
            $password = $model->where(array('id'=>$id))->getField('password');
            if($password == md5(123456)){
                $this->returnData('重置成功！',1);
            }

            $status = $model->where(array('id'=>$id))->setField('password',md5(123456));
            if($status){
                $this->returnData('重置成功！',1);
            }else{
                $this->returnData('重置失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /**
     * 清理缓存
     */
    private function clearCache($type){
        S('manage_mobile_list_'.$type,null);
    }
} 